package cn.jobstart.spider.downloader.request;

import cn.jobstart.common.StringUtil;
import cn.jobstart.spider.AbstractTalentSearch;
import cn.jobstart.spider.TalentJob51Search;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-11-13 09:43
 */
public class TalentJob51Request extends AbstractTalentRequest<TalentJob51Request, TalentJob51Search> {


    private static final String URL = "https://mallapi.51job.com/mall-portal/resumeSearch/list";

    public TalentJob51Request() {
        super(URL);
        super.setMethod(method());
    }

    @Override
    public String url() {
        return URL;
    }

    /**
     * 请求方式
     *
     * @return
     */
    @Override
    public String method() {
        return HttpConstant.Method.POST;
    }

    public void addHeader(){
        addHeader("Accept","application/json, text/plain, */*");
        addHeader("Accept-Language","zh-CN,zh;q=0.9");
        addHeader("Content-Type","application/json;charset=UTF-8");
        addHeader("Frompageurl","https://mall.51job.com/Revision/online/talent/search");
        addHeader("Origin","https://mall.51job.com");
        addHeader("Host","mallapi.51job.com");
        addHeader("Pageurl","https://mall.51job.com/Revision/online/talent/search");
        addHeader("Referer","https://mall.51job.com/");
        addHeader("Sec-Ch-Ua","\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"");
        addHeader("Sec-Ch-Ua-Mobile","?0");
        addHeader("Sec-Ch-Ua-Platform","Windows");
        addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
    }


    @Override
    public void buildExtra(TalentJob51Search talentJob51Search) {
        putExtra("pageIndex", talentJob51Search.getPageIndex());
        setRequestBody(HttpRequestBody.json(JSON.toJSONString(talentJob51Search), "utf-8"));
        addHeader();

    }

    @Override
    public HttpRequestBody getRequestBody() {
        HttpRequestBody body = super.getRequestBody();
        if (StringUtil.isNullOrEmpty(body)) {
            return body;
        }
        try {
            JSONObject bodyJson = JSONObject.parseObject(JSON.toJSONString(new String(body.getBody(), body.getEncoding())));
            bodyJson.put("_t", System.currentTimeMillis());
            return HttpRequestBody.json(bodyJson.toJSONString(), body.getEncoding());
        } catch (Exception e) {

        }

        return super.getRequestBody();
    }
}
